package org.exia.minisig.swing.view.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Interest;
import org.exia.minisig.model.object.Itinerary;
import org.exia.minisig.model.object.News;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.swing.controller.GUIManager;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.common.ImageNavigation;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class ItineraryNavigation extends JPanel {
	//VAR
	private ArrayList<Interest> interestOfItinerary;
	public ArrayList<Interest> getInterestOfItinerary() {
		return interestOfItinerary;
	}
	public void setInterestOfItinerary(ArrayList<Interest> interestOfItinerary) {
		this.interestOfItinerary = interestOfItinerary;
	}
	private DescriptibleElement element;
	private NavigationFram parent;
	public NavigationFram getParent() {
		return parent;
	}
	public void setParent(NavigationFram parent) {
		this.parent = parent;
	}
	private ArrayList<String> historique = new ArrayList<>();
	//CONSTRUTOR
	public ItineraryNavigation(DescriptibleElement _element, NavigationFram _parent){
		initializeVar(_element, _parent);
	}
	public void initializeVar(DescriptibleElement _element, NavigationFram _parent){
		
		this.setBackground(Color.white);
		this.parent = _parent;
		this.element = _element;
		loadListInterest();
		fillPanel();

	}
	public void loadListInterest(){
		interestOfItinerary = new ArrayList<>();
		for (Interest interest : parent.getPlace().getInterests()) {
			Itinerary selected = ((Interest)parent.getSelectedElement()).getItinerary();
			if (!interest.getItinerary().getLabel().equals("")){
			if (interest.getItinerary().getLabel().equals(selected.getLabel())){
				interestOfItinerary.add(interest);
			}
			}
		}
	}
	public void fillPanel(){
		this.removeAll();
		this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//PARAMETER
		gc.weightx=1;
		gc.weighty=1;
		//---------------------------------------//
		//LABEL
		gc.gridheight=1;
		gc.gridwidth= 4;
		gc.gridx=0;
		gc.gridy=0;
		gc.insets = new Insets(0, 0, 5, 0);
		JPanel panelTitre = new JPanel(new BorderLayout());
		
		JLabel titre = new JLabel(element.getLabel());
		
		panelTitre.setBackground(Color.white);
		titre.setBackground(Color.white);
		titre.setOpaque(true);
		
		titre.setVerticalAlignment(SwingConstants.CENTER);
		titre.setHorizontalAlignment(SwingConstants.CENTER);
		panelTitre.add(titre, BorderLayout.CENTER);
		this.add(panelTitre, gc);
		//--------------------------------RDC------------------------------//
		//ETAGE 01
		//BTN+
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=1;
		gc.insets = new Insets(0, 5, 0, 5);
		ImageNavigation nav = new ImageNavigation(parent.getSelectedElement());
		this.add(nav, gc);
	
		//ETAGE Historique 
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=4;
		gc.weighty = 2;
		JLabel label = new JLabel("Historique : ");
		this.add(label, gc);
		//
		gc.gridwidth = 4;
		gc.gridheight = 2;
		gc.gridx=0;
		gc.gridy=5;
		gc.weighty = 2;
		String dataHistorique[] = new String[]{ "", "", "", "", ""};
		for (int i = 0; i < historique.size(); i++) {
			if (i < 5){
				dataHistorique[i] = historique.get((historique.size()-1) - i);
			}
		}
		final JList<String> listeHistorique = new JList<>(dataHistorique);
		listeHistorique.setVisibleRowCount(5);
		JScrollPane panelListHis = new JScrollPane(listeHistorique);
		this.add(panelListHis, gc);
		
		//ETAGE URLS
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=7;
		gc.weighty = 2;
		JLabel label2 = new JLabel("POI du parcours : ");
		this.add(label2, gc);
		//
		gc.gridwidth = 4;
		gc.gridheight = 2;
		gc.gridx=0;
		gc.gridy=8;
		gc.weighty = 2;
		//
		String Data[] = new String[interestOfItinerary.size()];
		for (int i = 0; i < interestOfItinerary.size(); i++) {
			Data[i] = interestOfItinerary.get(i).getLabel();
		}
		final JList<String> listePOIParcrous = new JList<>(Data);
		listePOIParcrous.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
			}
			@Override
			public void mouseClicked(MouseEvent evt) {
				 @SuppressWarnings("unused")
				JList<?> list = (JList<?>)evt.getSource();
			        if (evt.getClickCount() == 2) {
			        	for (Interest x: interestOfItinerary) {
							if (x.getLabel().equals(listePOIParcrous.getSelectedValue())){
								parent.getMap().setSelectedElement(x);
								historique.add(x.getLabel());
								reload();
							}
						}
			        } 			
		        }
			});
		listePOIParcrous.setVisibleRowCount(5);
		JScrollPane panelListPOI = new JScrollPane(listePOIParcrous);
		this.add(panelListPOI, gc);
		///
		this.revalidate();
	}
	public void reload(){
		fillPanel();
	}
}
